//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by FernFlower decompiler)
//

package com.ruoyi.system.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.ruoyi.common.core.utils.uuid.IdUtils;
import com.ruoyi.common.security.utils.SecurityUtils;
import com.ruoyi.system.api.RemoteFileService;
import com.ruoyi.system.api.domain.SysUser;
import com.ruoyi.system.api.model.LoginUser;
import com.ruoyi.system.domain.TestFile;
import com.ruoyi.system.domain.ToolFile;
import com.ruoyi.system.domain.vo.QuestionVo;
import com.ruoyi.system.mapper.TestFileMapper;
import com.ruoyi.system.service.ISysUserService;
import com.ruoyi.system.service.ITestFileService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

@Service
public class TestFileServiceImpl implements ITestFileService {
    @Autowired
    private ISysUserService sysUserService;
    @Autowired
    private RemoteFileService remoteFileService;
    @Autowired
    private TestFileMapper testFileMapper;

    public String testFileUpload(List<QuestionVo> files) {
        HashMap<String, Object> reMap = new HashMap();

        try {
            reMap.put("aaaaa", "文件不为空");
            LoginUser loginUser = SecurityUtils.getLoginUser();
            Iterator var4 = files.iterator();

            while(var4.hasNext()) {
                QuestionVo testFile = (QuestionVo)var4.next();
                testFile.setUploadName(loginUser.getSysUser().getNickName());
                this.insertTestFile(testFile);
            }
        } catch (Exception var6) {
            var6.printStackTrace();
        }

        reMap.put("code", 200);
        reMap.put("msg", "成功");
        return JSONObject.toJSONString(reMap);
    }

    public int insertTestFile(QuestionVo file) {
        String orgNo = file.getOrgNo();
        List<String> orgNos = Arrays.asList(orgNo.split(","));
        Iterator var4 = orgNos.iterator();

        while(var4.hasNext()) {
            String s = (String)var4.next();
            QuestionVo fileItem = new QuestionVo();
            fileItem.setFileName(file.getFileName());
            fileItem.setFileUrl(file.getFileUrl());
            fileItem.setOrgNo(s);
            fileItem.setUploadTime(new Date());
            fileItem.setFileId(IdUtils.simpleUUID());
            SysUser sysUser = this.sysUserService.selectUserByUserName(SecurityUtils.getUsername());
            fileItem.setUploadName(sysUser.getNickName());
            this.testFileMapper.insertTestFile(fileItem);
        }

        return 1;
    }

    public List<TestFile> selectTestFileList(TestFile file) {
        return this.testFileMapper.selectTestFileList(file);
    }

    public TestFile selectTestFileByFileId(String fileId) {
        return this.testFileMapper.selectTestFileByFileId(fileId);
    }

    public int updateTestFile(ToolFile file) {
        return this.testFileMapper.updateTestFile(file);
    }

    public JSONObject deleteTestFileByIds(String ids) {
        String[] idArr = ids.split(",");
        boolean a = false;
        StringBuilder filePaths = new StringBuilder();
        String[] var5 = idArr;
        int var6 = idArr.length;

        for(int var7 = 0; var7 < var6; ++var7) {
            String fileId = var5[var7];
            TestFile file = this.testFileMapper.selectTestFileByFileId(fileId);
            String filePath = file.getFileUrl();
            filePaths.append(",").append(filePath);
            this.testFileMapper.deleteTestFileById(fileId);
        }

        JSONObject jsonObject = new JSONObject();
        jsonObject.put("filePath", filePaths.substring(1));
        return jsonObject;
    }
}
